
local huantu = fk.CreateSkill {
  name = "kl__huantu",
}


huantu:addEffect(fk.CardUseFinished, {
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(huantu.name) and data.card.suit ~= Card.NoSuit
    and not table.contains(player:getTableMark("@kl__huantu-turn"), data.card:getSuitString(true))
    and (player.room:getCardOwner(data.card) == player or player.room:getCardArea(data.card) == Card.Processing)
  end,
  on_cost = function (self, event, target, player, data)
    local choices = {"kl__huantu-top", "kl__huantu-bottom", "Cancel"}
    local choice = player.room:askToChoice(player, {
      skill_name = huantu.name,
      choices = choices,
      prompt = "#kl__huantu:::"..data.card:toLogString(),
    })
    if choice and choice ~= "Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    local id1 = data.card.id
    room:addTableMarkIfNeed(player, "@kl__huantu-turn", data.card:getSuitString(true))
    local id2
    if choice == "kl__huantu-top" then
      id2 = room:getNCards(1, "top")[1]
      room:sendLog{ type = "#kl__huantu-log", from = player.id, arg = Fk:getCardById(id2):toLogString() ,arg2 = "牌堆顶", toast = true }
    else
      id2 = room:getNCards(1, "bottom")[1]
      room:sendLog{ type = "#kl__huantu-log", from = player.id, arg = Fk:getCardById(id2):toLogString() ,arg2 = "牌堆底", toast = true }
    end

    local moves = {
      {
        ids = {id1},
        from = player,
        toArea = Card.Processing,
        moveReason = fk.ReasonPut,
        skillName = huantu.name,
        moveVisible = true,
      },
      {
        ids = {id2},
        toArea = Card.Processing,
        moveReason = fk.ReasonPut,
        skillName = huantu.name,
        moveVisible = true,
      }
    }
    room:sendLog{
      type = "#ShowPindianCard",
      from = player.id,
      arg = Fk:getCardById(id1):toLogString(),
    }
    room:moveCards(table.unpack(moves))
    room:sendFootnote({ id1 }, {
      type = "##PindianCard",
      from = player.id,
    })
    local winner
    if Fk:getCardById(id1).number > Fk:getCardById(id2).number then
      winner = player
    end
    room:sendLog{
      type = "#ShowPindianResult-huantu",
      from = player.id,
      to = {},
      arg = Fk:getCardById(id1):toLogString(),
      arg2 = Fk:getCardById(id2):toLogString(),
      arg3 = winner == player and "pindianwin" or "pindiannotwin",
      toast = true,
    }
    room:cleanProcessingArea({ id1, id2 }, huantu.name)

    if winner ~= player then
      room:damage{
        to = player,
        from = nil,
        damage = 1,
        skillName = huantu.name,
      }
    else
      player:drawCards(1, huantu.name)
      if player:isWounded() then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = huantu.name,
        }
      end
    end
  end,
})


return huantu
